package com.gcb.service.impl;

import com.gcb.constant.MessageConstant;
import com.gcb.dao.CheckGroupDao;
import com.gcb.entity.PageResult;
import com.gcb.entity.QueryPageBean;
import com.gcb.entity.Result;
import com.gcb.pojo.CheckGroup;
import com.gcb.pojo.CheckItem;
import com.gcb.service.CheckGroupService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 35235
 * @Date 2021/8/5 14:28
 * @Version 1.0
 */
@DubboService(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupDao checkGroupDao;

    @Override
    public PageResult queryQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage, pageSize);
        //select * from t_checkgroup limit 0,10
        Page<CheckGroup> checkGroups = checkGroupDao.selectByCondition(queryString);

        return new PageResult(checkGroups.getTotal(), checkGroups);
    }

    @Override
    public List<CheckItem> findAllFromCheckItem() {
        return checkGroupDao.findAllFromCheckItem();
    }


    @Override
    public Result add(Integer[] checkItemIds, CheckGroup checkGroup) {
        int count = checkGroupDao.insertCheckGroup(checkGroup);
        int checkGroupId = checkGroup.getId();
        boolean result = true;
        for (int checkItemId : checkItemIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("checkGroupId", checkGroupId);
            map.put("checkItemId", checkItemId);
            int count2 = checkGroupDao.insertCheckGroupCheckItem(map);
            if (count2 == 0) {
                result = false;
                System.out.println("关系表插入出错了");
            }
        }
        if (result == true) {
            return new Result(result, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } else {
            return new Result(result, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @Override
    public Result delete(Integer id) {
        int count = checkGroupDao.delete(id);
        if (0 == count) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        } else {
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
    }

    @Override
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        if (null == checkGroup) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        } else {
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS, checkGroup);
        }
    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        Integer[] checkitemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        if (0 == checkitemIds.length) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
        }
    }

    @Override
    public Result update(Integer[] checkitemIds, CheckGroup checkGroup) {
        int updateCount = checkGroupDao.updateCheckGroup(checkGroup);
        Map<String, Object> map = new HashMap<>();
        int deleteCount = checkGroupDao.deleteCheckGroupCheckItem(checkGroup.getId());
        int insertCount = 0;
        for (Integer checkitemId : checkitemIds) {
            map.put("checkItemId", checkitemId);
            map.put("checkGroupId", checkGroup.getId());
            insertCount = checkGroupDao.insertCheckGroupCheckItem(map);
        }

        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        CheckGroupDao checkGroupDao = (CheckGroupDao) context.getBean("checkGroupDao");
//        for (CheckItem checkItem : checkGroupDao.findAllFromCheckItem()) {
//            System.out.println(checkItem);
/*        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setCode("43435435");
        int i = checkGroupDao.insertCheckGroup(checkGroup);
        System.out.println(checkGroupDao.findCheckItemIdsByCheckGroupId(15));
        for (int i1 : checkGroupDao.findCheckItemIdsByCheckGroupId(15)) {
            System.out.println(i1);
        }*/
        Map<String, Object> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        list.add(31);
        list.add(55);
        list.add(34);
        list.add(46);
        map.put("checkgroupId", 32);
        map.put("checkitemIds", list);
        checkGroupDao.deleteCheckGroupCheckItem(30);
    }

}
